package org.itboys.coo.util;

import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;
import java.util.Date;

/**
 * 包名：com.hz.sunday.xlcl.utils
 * 作者 : 江雄
 * Email: empty90911@126.com
 * 时间：2016年1月3日 下午4:10:43
 * 描述:编号生成器
 */
public class NumberUtil {
    /**
     * 生成编号
     * @return
     */
    public static String generateNumber(){
        StringBuilder orderNo =new StringBuilder();
        Date now = new Date();
        String year = String.valueOf(now.getYear()+1900);
        String month= (now.getMonth()+1)<10?"0"+String.valueOf(now.getMonth()+1):String.valueOf(now.getMonth()+1);
        String day = now.getDate()<10?"0"+String.valueOf(now.getDate()):String.valueOf(now.getDate());
        //年月日
        orderNo.append(year);
        orderNo.append(month);
        orderNo.append(day);
        //毫秒时间戳后6位
        String times = String.valueOf(new Date().getTime());
        times=times.substring(times.length()-4,times.length());
        orderNo.append(times);
        //X位随机数
        String fourNumber = RandomUtil.generateRandomNumber(3);
        orderNo.append(fourNumber);
        return orderNo.toString();
    }
    /**
     * 生成编号
     * @return
     */
    public static String generateNumber(String prefix){
        StringBuilder orderNo =new StringBuilder();
        Date now = new Date();
        String year = String.valueOf(now.getYear()+1900);
        String month= (now.getMonth()+1)<10?"0"+String.valueOf(now.getMonth()+1):String.valueOf(now.getMonth()+1);
        String day = now.getDate()<10?"0"+String.valueOf(now.getDate()):String.valueOf(now.getDate());
        if(StringUtils.isNotEmpty(prefix)){
            orderNo.append(prefix);
        }

        //年月日
        orderNo.append(year);
        orderNo.append(month);
        orderNo.append(day);
        //毫秒时间戳后6位
        String times = String.valueOf(new Date().getTime());
        times=times.substring(times.length()-4,times.length());
        orderNo.append(times);
        //6位随机数
        String fourNumber = RandomUtil.generateRandomNumber(6);
        orderNo.append(fourNumber);
        return orderNo.toString();
    }

    /**
     * 生成作品编号
     * @param prefix
     * @return
     */
    public static String generateYefuNumber(String prefix){
        StringBuilder orderNo =new StringBuilder();
        if(StringUtils.isNotEmpty(prefix)){
            orderNo.append(prefix);
        }
        //毫秒时间戳后6位
        String times = String.valueOf(new Date().getTime());
        times=times.substring(times.length()-4,times.length());
        orderNo.append(times);
        //6位随机数
        String fourNumber = RandomUtil.generateRandomNumber(4);
        orderNo.append(fourNumber);
        return orderNo.toString();
    }

    /**
     * 生成用户顺序编码
     * @param number
     * @return
     */
    public static synchronized String generateYefuMember(Integer number){
        number++;
        Date date=new Date();
        String year="HF"+String.valueOf(date.getYear()).substring(1,3);
        DecimalFormat decimalFormat = new DecimalFormat("000000");
        return  year+decimalFormat.format(number.intValue());
    }
}
